package org.smallreunion.twitter;

import org.smallreunion.exception.TwitterRuntimeException;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.auth.AccessToken;
import twitter4j.auth.OAuthAuthorization;
import twitter4j.auth.RequestToken;
import twitter4j.conf.ConfigurationBuilder;

/**
 *
 * PokemonMS用TwitterAPI
 *
 * @author Sinmetal
 *
 */
public class Twitter4sr {

    /** Twitter */
    private Twitter twitter;

    @SuppressWarnings("unused")
    private Twitter4sr() {};

    /**
     * コンストラクタ
     */
    public Twitter4sr(String consumerKey, String consumerSecret) {
        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.setOAuthConsumerKey(consumerKey);
        builder.setOAuthConsumerSecret(consumerSecret);
        builder.setGZIPEnabled(false);
        OAuthAuthorization auth = new OAuthAuthorization(builder.build());

        twitter = new TwitterFactory().getInstance(auth);
    }

    /**
     * アクセストークン取得
     *
     * @return
     */
    public AccessToken getOAuthAccessToken() {
        try {
            return twitter.getOAuthAccessToken();
        } catch (TwitterException e) {
            throw new TwitterRuntimeException(e);
        }
    }

    /**
     * アクセストークン取得
     *
     * @param requestToken
     * @return
     */
    public AccessToken getOAuthAccessToken(RequestToken requestToken, String oauthVerifier) {
        try {
            return twitter.getOAuthAccessToken(requestToken, oauthVerifier);
        } catch (TwitterException e) {
            throw new TwitterRuntimeException(e);
        }
    }

    /**
     * User取得
     *
     * @return
     */
    public User getUser() {
        try {
            return twitter.verifyCredentials();
        } catch (TwitterException e) {
            throw new TwitterRuntimeException(e);
        }
    }

    /**
     * リクエストトークン作成
     *
     * @return
     */
    public RequestToken getOAuthRequestToken(String callbackURL) {
        try {
            return twitter.getOAuthRequestToken(callbackURL);
        } catch (TwitterException e) {
            throw new TwitterRuntimeException(e);
        }
    }
}
